//
//  UserNotificationPowerManager.swift
//  RNAsmanMaster
//
//  Created by 李旭峰 on 7/23/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit
import UserNotifications

class UserNotificationPowerManager: NSObject {
    
    /// UNAuthorizationStatus
    ///
    /// - Parameter didCompleted: didCompleted description
    /// 用户尚未就应用程序是否可以发布用户通知做出选择
    ///     notDetermined
    /// 该应用程序无权发布用户通知.
    ///     denied
    /// 该应用程序被授权发布用户通知.
    ///     authorized
    ///     provisional
    @objc static func settingsStatus(didCompleted:@escaping (_ status:UNAuthorizationStatus) -> ()) {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            didCompleted(settings.authorizationStatus)
        }
    }
    @objc static func getSettingsStatus(didCompleted:@escaping (_ isAuthorized:Bool)->()) {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            switch settings.authorizationStatus {
            case .notDetermined,.denied:
                didCompleted(false)
            default:didCompleted(true)
            }
        }
    }
    @objc static func getSettings(didCompleted:@escaping (_ settings:UNNotificationSettings) -> ()) {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            didCompleted(settings)
        }
    }
    @objc static func switchToSetting(){
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
          DispatchQueue.main.async {
            switch settings.authorizationStatus {
            case .notDetermined:
              switchToSettingTips(didCompleted: { (isGranted, error) in })
            default:switchToAppSettings(didCompleted: { (isSuccess) in })
            }
          }
        }
    }
    /// 用户尚未选择时，调用获取授权
    @objc static func switchToSettingTips(didCompleted:@escaping (_ granted:Bool,_ error:Error?)->()){
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
            didCompleted(granted,error)
        }
    }
    /// 用户关闭通知时，跳转到设置页面
    @objc static func switchToAppSettings(didCompleted:@escaping (_ isSuccess:Bool) -> ()){
        let url = URL(string: UIApplication.openSettingsURLString)!
        UIApplication.shared.open(url, options: [:]) { (isSuccess) in
            didCompleted(isSuccess)
        }
    }
  @objc static func addAlias(aliasNo:String,didCompleted:@escaping (_ granted:Bool,_ error:Error?)->()){
//    CloudPushSDK.addAlias(aliasNo) { (result) in
//      didCompleted(result?.success ?? false,result?.error)
//    }
  }
  @objc static func removeAlias(aliasNo:String,didCompleted:@escaping (_ granted:Bool,_ error:Error?)->()){
//    CloudPushSDK.removeAlias(aliasNo) { (result) in
//      didCompleted(result?.success ?? false,result?.error)
//    }
  }
  @objc static func syncBadgeNum(badgeNum:Int,didCompleted:@escaping (_ granted:Bool,_ error:Error?)->()){
//    CloudPushSDK.syncBadgeNum(UInt(badgeNum)) { (result) in
//      didCompleted(result?.success ?? false,result?.error)
//      DispatchQueue.main.async {
//        if result?.success ?? false {
//          UIApplication.shared.applicationIconBadgeNumber = badgeNum
//        }
//      }
//    }
  }
  
}
